package com.bea.ftd.routing.zuul;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import com.bea.ftd.routing.filter.*;
import com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.properties.RateLimitProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import com.google.gson.Gson;
import com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.repository.DefaultRateLimiterErrorHandler;
import com.marcosbarbero.cloud.autoconfigure.zuul.ratelimit.config.repository.RateLimiterErrorHandler;

@EnableConfigurationProperties(RateLimitProperties.class)
@Configuration
public class MyRoutingConfig {

    @Autowired
    ZuulProperties zuulProperties;

    @Autowired
    ServerProperties server;

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Bean
    public MyRouteLocator routeLocator() {
        MyRouteLocator routeLocator = new MyRouteLocator("/", this.zuulProperties);
        routeLocator.setJdbcTemplate(jdbcTemplate);
        return routeLocator;
    }

    @Bean
    public MsgMappingFilter msgMappingFilter(){
        return new MsgMappingFilter();
    }

    @Bean
    public MsgInputFilter msgInputFilter(){
        return new MsgInputFilter();
    }

//    @Bean
//    public RateLimitFilter rateLimitFilter(){  }

    @Bean
    public ErrorFilter errorFilter(CustomErrorHandler errorHandler) {
        ErrorFilter errorFilter = new ErrorFilter();
        errorFilter.setErrorHandler(errorHandler);
        return errorFilter;
    }



    @Bean
    public RateLimiterErrorHandler rateLimitErrorHandler() {
        return new DefaultRateLimiterErrorHandler() {
            @Override
            public void handleSaveError(String key, Exception e) {

            }

            @Override
            public void handleFetchError(String key, Exception e) {
                // custom code
            }

            @Override
            public void handleError(String msg, Exception e) {
                // custom code
            }
        };
    }



    @Component
    public class CustomErrorHandler implements ResponseHandler {


        private String errorMessage = "connect error";

        @Override
        public int getResponseCode() {
            return HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
        }

        @Override
        public String getResponseBody(String originMessage, Throwable e) {
            Gson gson = new Gson();
            Map<String, Object> result = new HashMap<String, Object>();
            result.put("status", HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            result.put("message", errorMessage);
            return gson.toJson(result);
        }
    }

}
